<?php 
namespace app\adm\controller;

/**
 * 
 */
class Trade extends Base{
	
	// function __construct(argument)
	// {
	// 	# code...
	// }
	public function index ()
	{
		if (is_post()) {
			$post = input('post.');
			if (input('post.id')) {
				if (input('get.acc') == 'del') {
					$rst = db('trade')->where('id',$post['id'])->delete();
					if ($rst) {
						$this->success('删除成功');
					}
					$this->error('删除失败');
				}
				$rst = db('trade')->where('id',$post['id'])->update($post);
				if ($rst) {
					$this->success('修改成功');
				}
				$this->error('修改失败');
			}
			$post['time'] = date('Y-m-d',time());
			$rst = db('trade')->insert($post);
			if ($rst) {
				$this->success('添加成功');
			}
			$this->error('请稍后重试');
		}
		if (is_get()) {
			if (input('get.id')) {
				$data = db('trade')->where('id',input('get.id'))->find();
				return $data;
			}
			$list = db('trade')
				-> paginate()
				-> each(function($items,$key){
					$items['name']=db('tr_group')->where('id',$items['cid'])->value('name');
					return $items;
			});
			$group = db('tr_group')->select();
			$items = [];
			foreach ($group as $k=>$v) {
				$items[$v['id']] = $v['name'];
			}
			$this->assign('items',$items);
			$this->assign('list',$list);
			return $this->fetch();
		}
	}

	public function group ()
	{
		$rst = db('tr_group')->insert(input('post.'));
		if ($rst) $this->success('添加成功');$this->error('请稍后重试');
	}
}